package com.schoolegard.services.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.schoolegard.services.CatalogService;
import com.schoolgard.dao.CatalogDao;
import com.schoolgard.dao.RolePrivilegeDao;
import com.schoolgard.entitybean.Catalog;
import com.schoolgard.entitybean.RolePrivilege;

public class CatalogServiceImpl implements CatalogService {
	
	private static Logger logger = Logger.getLogger(CatalogServiceImpl.class);
	private CatalogDao catalogDao;
	private RolePrivilegeDao rolePrivilegeDao;
	public void setCatalogDao(CatalogDao catalogDao) {
		this.catalogDao = catalogDao;
	}
	public void setRolePrivilegeDao(RolePrivilegeDao rolePrivilegeDao) {
		this.rolePrivilegeDao = rolePrivilegeDao;
	}

	@Override
	public List<Catalog> parseCatalog(String catalogIds) {
		return parseCatalog(catalogIds , 1); // 1标示从id为3位的节点，即是顶级节点
	}
	@Override
	public List<Catalog> parseCatalog(String catalogIds, int beginLevel) {
		return parseCatalog(catalogIds, beginLevel,4); //由于菜单只支持4层节点，顾4为最大节点层数
	}
	@Override
	public List<Catalog> parseCatalog(String catalogIds, int beginLevel,
			int endLevel) {
		String[] ida = null;
		List<Catalog> catalog = new ArrayList<Catalog>();
		if(StringUtils.isNotBlank(catalogIds)){
			 ida = catalogIds.split(",");
		}
		for (String id : ida) {
			int level = id.length()/3;
			if(level >= beginLevel && level <= endLevel){
				Catalog clg = catalogDao.findById(id.trim());
				if(clg != null) {
					catalog.add(catalogDao.findById(id.trim()));
				}
			}
		}
		sortCatalog(catalog);
		return catalog;
	}
	
	private void sortCatalog(List<Catalog> catalog)
	{
	  ComparatorCatalog sort = new ComparatorCatalog();
	  Collections.sort(catalog, sort);
	}
	/**
	 * 排序
	 * @author shamo
	 *
	 */
   class ComparatorCatalog implements Comparator {
		@Override
		public int compare(Object o1, Object o2) {
			Catalog catalog1 = (Catalog) o1;
			Catalog catalog2 = (Catalog) o2;
			int flag = catalog1.getSort().compareTo(catalog2.getSort());
			return flag;
		}

	}
	
	@Override
	public List<Catalog> findCatalogByLevel(int level) {
		String hql = "from Catalog where length(id) = "+level*3+" and id != 000 order by sort";
		return catalogDao.find(hql);
	}
	@Override
	public List<Catalog> findCatalogByParentId(String parentId) {
		String hql = "from Catalog where id like '"+parentId+"%' and length(id) = "+(parentId.length()+3) +"  order by sort , id";
		if(StringUtils.equals(parentId, "000")){
			hql = "from Catalog where length(id) = 3 and id != '000' order by sort , id";
		}
		return catalogDao.find(hql);
	}
	@Override
	public Catalog findById(String catalogId) {
		return catalogDao.findById(catalogId);
	}
	@Override
	public String saveOrUpdate(Catalog catalog) {
		String catalogId = "0";
		try{
		    catalogId = (catalogDao.createOrModify(catalog)).getId();
		}catch (Exception e) {
			e.printStackTrace();
			logger.error("保存菜单表失败！",e);
			return "0";
		}
		return catalogId;
	}
	@Override
	public int delete(Catalog catalog) {
		try{
			catalogDao.remove(catalog);
		}catch (Exception e) {
			logger.error("删除失败！",e);
			return 0;
		}
		return 1;
	}
	@Override
	public List<RolePrivilege> findByRoleId(Long roleId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(RolePrivilege.class);
		criteria.add(Restrictions.eq("role.id", roleId));
		return rolePrivilegeDao.findByCriteria(criteria);
	}
	@Override
	public void saveRolePrivilege(RolePrivilege roleprivilege) {
		rolePrivilegeDao.createOrModify(roleprivilege);
	}
	@Override
	public int findMaxCIdByParentId(String parentId) {
		String hql = "select max(id) from Catalog where id like '"+parentId+"%' and length(id) = "+(parentId.length()+3) +"  order by id";
		if(StringUtils.equals(parentId, "000")){
			hql = "select max(id) from Catalog where length(id) = 3 and id != '000' order by id";
		}
		Object obj = catalogDao.find(hql);
		int num = 0;
		if(obj != null){
			String numStr = obj.toString();
		 	num = Integer.parseInt(numStr.substring(numStr.length()-3,numStr.length()-1));
		}
		return num;
	}
}
